<?php
namespace PsiFramework\Classes\Util\Style\DAO;

import("Classes.DAO.AbstractDAO");
import("Classes.Util.Style.Model.Style");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Classes\Util\Database\Filters\QueryFilter;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlFunction;
use \PsiFramework\Classes\Util\Database\Query\SqlOperation;
use \PsiFramework\Classes\Util\Database\Query\SqlValue;
use \PsiFramework\Classes\Util\Style\Model\Style;

/**
 * AlloMedia
 * Portail des professionnels
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The StyleDAO is the Data Access Object for the class Style
 *
 * @package DAO
 */
class StyleDAO extends AbstractDAO
{

    protected static $_singleton = null;

    public function retrieveByPartialName(
        /*string*/ $partialName,
        /*bool*/ $exactName=false
    )
    {

        if ($exactName && isNullOrEmptyString($partialName)) {
            return null ;
        }

        $colNom = new SqlColumn("style_name");

        $value = mb_strtoupper($partialName, 'utf-8');
        if (!$exactName) {
            $value = '%' . $value . "%" ;
        }

        $sqlValue = new SqlValue($value);

        $upper = new SqlFunction("UPPER");
        $upper->addParameter($colNom);

        $op = null ;
        if ($exactName) {
            $op = new SqlOperation(SqlOperation::$equal);
        } else {
            $op = new SqlOperation(SqlOperation::$like);
        }

        if (!is_null($op)) {
            $op->addParameter($upper);
            $op->addParameter($sqlValue);

            $result = $this->retrieveAll(new QueryFilter($op));

            if (!$exactName) {
                return $result ;
            } else if (count($result)==1) {
                return $result[0] ;
            } else {
                return null ;
            }
        } else {
            return null ;
        }

    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return StyleDAO    instance
     */
    public static function getInstance($objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new StyleDAO($objectParameters);
        }

        return self::$_singleton;
    }

}
